import 'package:boss/models/commodity.dart';
import 'package:boss/models/store_menu.dart';
import 'package:boss/provider/commodity_provider.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/widget/button/radius_button.dart';
import 'package:boss/ui/widget/custom_divier.dart';
import 'package:boss/ui/widget/custom_tab.dart';
import 'package:boss/ui/widget/modal/modal.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

showChangeClassificationModal(BuildContext context) {
  showModal(
    context,
    builder: (ctx) => _ChangeClassificationModal(),
  );
}

class _ChangeClassificationModal extends StatefulWidget {
  @override
  _ChangeClassificationModalState createState() => _ChangeClassificationModalState();
}

class _ChangeClassificationModalState extends State<_ChangeClassificationModal> {
  StoreMenu selectedMenu;
  List<StoreMenu> menus;
  int type;

  @override
  void initState() {
    super.initState();

    var commodityProvider = Provider.of<CommodityProvider>(
      context,
      listen: false,
    );

    var isPackage = commodityProvider.selectedCommodities[0].commodityType == Commodity.COMMODITY_TYPE_PACKAGE;
    type = isPackage ? 1 : 0;
    menus = commodityProvider.storeMenus.where((item) => item.isPackages == type).toList();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 580.w,
      height: 640.w,
      padding: EdgeInsets.symmetric(
        horizontal: 20.w,
        vertical: 30.w,
      ),
      child: Column(
        children: <Widget>[
          Expanded(
            child: CustomTab(
              isScrollable: false,
              tabs: ['改入分类选项'],
              tabViews: <Widget>[
                Container(
                  margin: EdgeInsets.only(
                    top: 30.w,
                  ),
                  child: ListView.separated(
                    padding: EdgeInsets.zero,
                    itemBuilder: (context, index) {
                      var current = menus[index];
                      var isSelected = current == selectedMenu;

                      return GestureDetector(
                        onTap: () {
                          setState(() {
                            selectedMenu = current;
                          });
                        },
                        behavior: HitTestBehavior.translucent,
                        child: Container(
                          height: 90.w,
                          alignment: Alignment.center,
                          child: Text(
                            current.menuName,
                            style: isSelected ? TextStyleConstant().origin_28 : TextStyleConstant().normal_1_28,
                          ),
                        ),
                      );
                    },
                    separatorBuilder: (context, index) {
                      return CustomDivider(
                        margin: EdgeInsets.symmetric(
                          horizontal: 50.w,
                        ),
                      );
                    },
                    itemCount: menus.length,
                  ),
                ),
              ],
            ),
          ),
          Container(
            margin: EdgeInsets.only(
              top: 40.w,
            ),
            child: RadiusButton(
              title: '确 认',
              onTap: () async {
                var commodityProvider = Provider.of<CommodityProvider>(
                  context,
                  listen: false,
                );
                var result = await commodityProvider.changeCommodityMenu(selectedMenu);
                if (result) {
                  commodityProvider.setBatchType(BatchType.normal);
                  Navigator.pop(context);
                }
              },
              width: 500.w,
            ),
          ),
        ],
      ),
    );
  }
}
